/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-08-15 10:57:09
 * @LastEditors: TT
 * @LastEditTime: 2023-10-30 11:14:27
 */

import 'package:flutter/material.dart';
import 'package:hzy_project_template/commpontents/commpontents_index.dart';
import 'package:hzy_project_template/config/config_index.dart';
import 'package:hzy_project_template/pages/login/vm/login_vm.dart';
import 'package:hzy_project_template/utils/utils_index.dart';
import 'package:hzy_project_template/base_ui_config/base_ui_config_index.dart';
import '../widgets/compontents_index.dart';
import '../routers/routers_index.dart';
import '../tools/tools_index.dart';

class NoteLoginV extends CommonGetXV<NoteLoginC> {
  NoteLoginV({Key? key}) : super(key: key);
  @override
  NoteLoginC get controller => Get.put(NoteLoginC());

  @override
  IconData? createLeadIcon() {
    return Icons.close;
  }

  @override
  Widget createBody({
    required BuildContext context,
    BoxConstraints? constraints,
  }) {
    Widget body = Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        createTopInfoWidget(),
        createBottomInfoWidget(),
      ],
    );

    body = Stack(
      children: [
        body,
        Positioned(
          top: SizeMacro.statusBarHeight,
          left: 10,
          child: IconButton(
            onPressed: () {
              currentGoback();
            },
            icon: Icon(
              createLeadIcon(),
              color: Colors.black,
            ),
          ),
        )
      ],
    );
    return body;
  }

  /// 创建顶部信息界面
  createTopInfoWidget() {
    Widget body = LoginCompontentS.configLoginHeaderWidget();
    return body;
  }

  /// 创建底部内容
  createBottomInfoWidget() {
    Widget body = Column(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        createOnceLoginWidget(),
        Gaps.vGap12,
        createAccountLoginWidget(),
        Gaps.vGap10,
        LoginAgreementWidget(
          key: controller.agreeKey,
          tapBoolCallback: (yorn) {
            controller.tapAgree(
              yorn: yorn,
            );
          },
        ),
      ],
    );

    body = Padding(
      padding: EdgeInsets.symmetric(
        horizontal: 20.w,
      ),
      child: body,
    );

    return body;
  }

  /// 创建一键登录
  createOnceLoginWidget() {
    Widget body = configOnlyTextBtnWidget(
      text: NoteLauncIdConfig.onceLogin.tr,
      tapCall: () {
        controller.tapOnceLogin();
      },
    );
    return body;
  }

  /// 创建账号密码登录
  createAccountLoginWidget() {
    Widget body = configOnlyTextBtnWidget(
      text: NoteLauncIdConfig.accountLogin.tr,
      bgColor: loginGaryColor,
      textColor: loginBlackColor,
      tapCall: () {
        controller.tapAccountLogin();
      },
    );
    return body;
  }

  @override
  bool configIsShowAppBar() {
    return false;
  }

  @override
  bool configSafeAreaTop() {
    return false;
  }
}

class NoteLoginC extends CommonGetXController {
  // --------- 属性 --------- //
  bool isAgree = false;
  GlobalKey<LoginAgreementState> agreeKey = GlobalKey();
  LoginVM vm = LoginVM();
  // --------- 生命周期  --------- //

  @override
  configDefault() {
    isShowAppBar = false;
    safeAreatop = false;

    scallBackGroundColor = Colors.white;
    safeAreabottm = true;
  }
  // --------- 数据处理  --------- //

  // --------- 触发事件  --------- //

  /// 配置点击用户协议

  tapAgree({
    required bool yorn,
  }) {
    isAgree = yorn;
  }

  /// 点击一键登录
  tapOnceLogin() async {
    isAgree = await LoginModuleTool.configAgreeIsShowPopWidget(
      yorn: isAgree,
    );
    agreeKey.currentState?.configIsTapAgreement(
      yorn: isAgree,
    );
    if (isAgree) {
      var yorn = await LoginCompontentS.configOnceLoginPop();
      if (yorn == true) {
        vm.configLogin(
          account: "火之夜",
          pwd: "111111",
        );
      }
    }
  }

  /// 点击账号登录
  tapAccountLogin() async {
    if (!isAgree) {
      isAgree = await LoginModuleTool.configAgreeIsShowPopWidget(
        yorn: isAgree,
      );
      agreeKey.currentState?.configIsTapAgreement(
        yorn: isAgree,
      );
      currentToPage(
        name: NoteLoginModuleRouterS.accountLogin,
      );
    } else {
      currentToPage(
        name: NoteLoginModuleRouterS.accountLogin,
      );
    }
  }
}
